<?php


namespace app\common\model;

use app\common\model\Station;
use think\Model;
use think\Db;

class Park extends Model
{
    protected $table = 'store_park';


    public function add($param)
    {
        $find = self::where(['sn' => $param['sn']])->find();
        if (empty($find)) {
            $Station = new Station();
            $param['station_id'] = $Station::where(['user_id' => $param['user_id']])->value("id");
            if (empty($param['station_id'])) {
                return '请设置您的电站！';
            }
            $param['images'] = str_replace(',', '|', $param['images']);
            $param['map_image'] = $param['images'];

            $param['create_at'] = date('Y-m-d H:i:s');
            self::insert($param);
            return 1;
        } else {
            return '编号重复,请勿重复提交！';
        }
    }

    public function park_save($param)
    {
        $user_id = $param['user_id'];
        $find = self::where(['sn' => $param['sn']])->where("user_id!=$user_id")->find();
        if (empty($find)) {
            $row = self::where(["id" => $param['id']])->find();
            if ($row) {
                if ($row['user_id'] == $param['user_id']) {
                    $Station = new Station();
                    unset($param['rate']);
                    unset($param['brand']);
                    unset($param['delete_time']);
                    $param['images'] = str_replace(',', '|', $param['images']);
                    $param['map_image'] = $param['images'];

                    $param['station_id'] = $Station::where(['user_id' => $param['user_id']])->value("id");
                    $param['create_at'] = date('Y-m-d H:i:s');
                    self::where(["id" => $param['id']])->update($param);
                    return 1;
                } else {
                    return '这不是您的车位,请勿提交！';
                }
            } else {
                return '车位未找到！';
            }
        } else {
            return '编号重复,请勿重复提交！';
        }
    }

    public function index($param)
    {
        $page = $param['page'] ?? 1;
        $limit = $param['limit'] ?? 100;
        $page = ($page - 1) * $limit;
        $status = $param['status'] ?? 0;

        $type = $param['type'] ?? 0;

        $where = "";
        if ($status) {
            $where = "status = '$status'";
        }
        $list = self::where(['user_id' => $param['user_id']])->where("delete_time is null")->where($where)->order("id desc")->limit($page, $limit)->select();
        $arr = [];
        foreach ($list as $k => &$v) {
            $v['images'] = str_replace('|', ',', $v['images']);
            if ($type == 1) {
                $have = Db::table("store_device")->where(['park_id' => $v['id']])->field("id")->find();
                if (empty($have)) {
                    array_push($arr, $v);
                }
            } else {
                array_push($arr, $v);
            }
        }
        return $arr;
    }

    public function del($param)
    {
        $find = self::where(['id' => $param['id']])->where(["user_id" => $param['user_id']])->find();
        if (empty($find)) {
            return '车位未找到！';
        } else {
            self::where(['id' => $param['id']])->where(["user_id" => $param['user_id']])->update(['delete_time' => date('Y-m-d H:i:s')]);
            return 1;
        }
    }


    public function collect($param)
    {
        $is_collect = Db::table("store_member_collect")->where("type=2")->where(["user_id" => $param['user_id'], "table_id" => $param['id']])->find();
        if ($is_collect) {
            Db::table("store_member_collect")->where(["user_id" => $param['user_id'], "table_id" => $param['id']])->delete();
            return '取消收藏';
        } else {
            Db::table("store_member_collect")->insert([
                'type' => 2, 'table_id' => $param['id'], "user_id" => $param['user_id'], 'add_time' => date('Y-m-d H:i:s')
            ]);
            return '收藏成功';
        }

    }

}